//
//  UIViewController+WHTopViewController.m
//  LvZhongMagicKey
//
//  Created by 邓伟浩 on 2018/3/16.
//  Copyright © 2018年 邓伟浩. All rights reserved.
//

#import "UIViewController+WHTopViewController.h"

@implementation UIViewController (WHTopViewController)

+ (UIViewController *)topViewControllerWithRootViewController:(UIViewController*)rootViewController
{
    if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tabBarController = (UITabBarController *)rootViewController;
        return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
    } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController* navigationController = (UINavigationController*)rootViewController;
        return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
    } else if (rootViewController.presentedViewController) {
        UIViewController* presentedViewController = rootViewController.presentedViewController;
        return [self topViewControllerWithRootViewController:presentedViewController];
    } else {
        return rootViewController;
    }
}

+ (UIViewController *)topViewController {
    UIViewController *controller = [self topViewControllerWithRootViewController:[UIApplication sharedApplication].delegate.window.rootViewController];
    return controller;
}

@end
